<template>
    <van-button v-if="isFollowed" @click="onFollow" class="follow-btn" round size="small" :loading="loading">已关注</van-button>
    <van-button v-else @click="onFollow" class="follow-btn" type="info" color="#3296fa" round size="small" icon="plus" :loading="loading">关注</van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user.js'
export default {
  name: '',
  props: {
    isFollowed: {
      type: Boolean,
      required: true,
    },
    userId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  data() {
    return {
      loading: false,
    }
  },
  methods: {
    async onFollow() {
      this.loading = true
      try {
        if (this.isFollowed) {
          // 已经关注，取消关注
          await deleteFollow(this.userId)
        } else {
          // 没有关注，关注
          await addFollow(this.userId)
        }
        // this.isFollowed = !this.isFollowed
        this.$emit("update:isFollowed",!this.isFollowed)
      
      } catch (err) {
        //   console.log(err);
        let msg = '关注失败'
        if (err.response && err.response.status === 400) {
          msg = '你不能关注你自己'
        }
        this.$toast(msg)
      }
      this.loading = false
    },
  },
  created() {},
}
</script>

<style lang="" scoped>
</style>